home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 59267 / 59267.xpi / chrome / content / autohidetabbar.js next >
Text File  |  2010-01-25  |  7KB  |  234 lines

  1. var autoHideTabBar12 = {
  2. start : function()
  3. {
  4.    this.prefService = Components.classes["@mozilla.org/preferences-service;1"]
  5.                .getService(Components.interfaces.nsIPrefService);
  6.  
  7.    this.readPrefs();
  8.  
  9.    var theBrowser = getBrowser();
  10.  
  11.    // Move the tab bar to the bottom?
  12.    if (this.isTabbarAtBottom)
  13.    {
  14.       // Move tab bar to the bottom of the main window.
  15.       var pc = theBrowser.mPanelContainer;
  16.       pc.parentNode.appendChild(theBrowser.mStrip);
  17.       pc.parentNode.appendChild(theBrowser.mTabDropIndicatorBar);
  18.    }
  19.  
  20.    // Add an item to the context menu of the tabbar.
  21.    this.addContextMenu();
  22.  
  23.    // add event handlers for tabbar.
  24.    // mouse is out of tab bar.
  25.    theBrowser.mStrip.addEventListener("mouseout", 
  26.          function(evt){ autoHideTabBar12.tryHideTabBar(); }, false);
  27.  
  28.    // the following 4 events imply the mouse is in tab bar.
  29.    theBrowser.mStrip.addEventListener("mouseover", 
  30.          function(evt){ autoHideTabBar12.isMouseInTabbar = true; }, false);
  31.  
  32.    theBrowser.mStrip.addEventListener("mousemove", 
  33.          function(evt){ autoHideTabBar12.isMouseInTabbar = true; }, false);
  34.  
  35.    theBrowser.mStrip.addEventListener("click", 
  36.          function(evt){ autoHideTabBar12.isMouseInTabbar = true; }, false);
  37.  
  38.    theBrowser.mStrip.addEventListener("dragenter", 
  39.          function(evt){ autoHideTabBar12.isMouseInTabbar = true; }, false);
  40.  
  41.    // when right click on the tab-bar.
  42.    // the following 2 events happen when tab context menu popup and hide.
  43.    theBrowser.mStrip.addEventListener("popupshowing", 
  44.          function(evt){ autoHideTabBar12.isContextMenuOn = true; }, false);
  45.  
  46.    theBrowser.mStrip.addEventListener("popuphidden", 
  47.          function(evt)
  48.          { 
  49.             autoHideTabBar12.isContextMenuOn = false;
  50.             autoHideTabBar12.tryHideTabBar();
  51.          },
  52.          false);
  53.  
  54.    // mouse is in the page. definitely out of tab bar.
  55.    theBrowser.mPanelContainer.addEventListener("mousemove", 
  56.          function(evt){ autoHideTabBar12.tryHideTabBar(); }, false);
  57. },
  58.  
  59. // Add an item to the context menu of the tabbar.
  60. addContextMenu : function()
  61. {
  62.    // See firefox source code "tabbrowser.xml" - the 2nd child
  63.    // of <anonid="strip"> is <anonid="tabContextMenu">
  64.    var contxtMenu = getBrowser().mStrip.childNodes[1];
  65.  
  66.    if (contxtMenu)
  67.    {
  68.       contxtMenu.appendChild(document.createElement("menuseparator"));
  69.       contxtMenu.appendChild(document.getElementById("ahtb12ContextAutohide"));
  70.    }
  71. },
  72.  
  73. // When mouse move from one tab to another inside the tab-bar,
  74. // mouseout and mouseover are fired and bubbled to the Strip.
  75. // So we delay hide-tabbar upon mouseout to see whether mouseover
  76. // will be received.
  77. tryHideTabBar : function()
  78. {
  79.    this.isMouseInTabbar = false;
  80.  
  81.    if (getBrowser().getStripVisibility())
  82.    {
  83.       // tab bar visible, go hide it, but postpone a little.
  84.       setTimeout(function() { autoHideTabBar12.hideTabBar(); }, 800);
  85.    }
  86. },
  87.  
  88. // Time to hide the tab-bar.
  89. hideTabBar : function()
  90. {
  91.    // feature is enabled?
  92.    if (!this.isEnabled)
  93.       return;
  94.  
  95.    // Wont hide when user invoke context menu.
  96.    if (this.isContextMenuOn)
  97.       return;
  98.  
  99.    // mouse back to tab-bar again?
  100.    if (this.isMouseInTabbar)
  101.       return;
  102.  
  103.    // Now, we can hide it.
  104.    var theBrowser = getBrowser();
  105.  
  106.    // get a fake tabbar for restoring the real tabbar.
  107.    var fakeTabbar = document.getElementById(this.fakeTabbarId);
  108.    if (!fakeTabbar)
  109.    {
  110.       // Add a fake tab-bar.
  111.       fakeTabbar = document.createElement("toolbar");
  112.       fakeTabbar.id = this.fakeTabbarId;
  113.       fakeTabbar.setAttribute("customizable", "false");
  114.       fakeTabbar.style.minHeight = "3px";
  115.       fakeTabbar.style.height = "3px";
  116.  
  117.  
  118.       var strp = theBrowser.mStrip;
  119.          strp.parentNode.insertBefore(fakeTabbar, strp);
  120.  
  121.       fakeTabbar.addEventListener("mouseover",
  122.             function() { autoHideTabBar12.showTabBar(); }, false);
  123.       fakeTabbar.addEventListener("dragenter",
  124.             function() { autoHideTabBar12.showTabBar(); }, false);
  125.    }
  126.  
  127.    if (fakeTabbar)
  128.    {
  129.       // show the fake tab-bar.
  130.       fakeTabbar.setAttribute("moz-collapsed", "false");
  131.  
  132.       // hide the tab-bar
  133.       //this.animateUpTabbar();
  134.       theBrowser.setStripVisibilityTo(false);
  135.    }
  136. },
  137.  
  138. showTabBar : function()
  139. {
  140.    var theBrowser = getBrowser();
  141.  
  142.    // show the tab-bar
  143.    // if there is only 1 tab and "Always show the tab bar" option
  144.    // is not checked, do not show the tab bar.
  145.    if (theBrowser.tabContainer.childNodes.length > 1 ||
  146.          !this.prefService.getBoolPref("browser.tabs.autoHide"))
  147.    {
  148.       theBrowser.setStripVisibilityTo(true);
  149.  
  150.       this.isMouseInTabbar = true;
  151.    }
  152.  
  153.    var fakeTabbar = document.getElementById(this.fakeTabbarId);
  154.    if (fakeTabbar)
  155.    {
  156.       // hide the fake tab-bar
  157.       fakeTabbar.setAttribute("moz-collapsed", "true");
  158.    }
  159. },
  160.  
  161. readPrefs : function()
  162. {
  163.    // Get the preferences
  164.    myprefs = this.prefService.getBranch("extensions.autohidetabbar.");
  165.  
  166.    this.isEnabled = myprefs.getBoolPref("isEnabled");
  167.    this.isTabbarAtBottom = myprefs.getBoolPref("isTabbarAtBottom");
  168.  
  169.    // set the context menu.
  170.    document.getElementById("ahtb12ContextAutohide").setAttribute(
  171.         "checked", this.isEnabled ? "true" : "false");
  172.  
  173.    // show/hide tab bar.
  174.    if (this.isEnabled)
  175.       this.tryHideTabBar();
  176.    else
  177.       this.showTabBar();
  178. },
  179.  
  180. toggleAutoHide : function()
  181. {
  182.    this.isEnabled = !this.isEnabled;
  183.  
  184.    // set the context menu.
  185.    document.getElementById("ahtb12ContextAutohide").setAttribute(
  186.         "checked", this.isEnabled ? "true" : "false");
  187.  
  188.    // save to Preferences.
  189.    var myprefs = this.prefService.getBranch("extensions.autohidetabbar.");
  190.    var defprefs = this.prefService.getDefaultBranch("extensions.autohidetabbar.");
  191.  
  192.    var prefname = "isEnabled";
  193.  
  194.    if (defprefs.getBoolPref(prefname) != this.isEnabled)
  195.       myprefs.setBoolPref(prefname, this.isEnabled);
  196.    else if (myprefs.prefHasUserValue(prefname))  // equal to default.
  197.       myprefs.clearUserPref(prefname);           // clear user's setting.
  198. },
  199.  
  200. // Not good. Not used.
  201. XXanimateUpTabbar : function()
  202. {
  203.    if (0 == this.animationAmount)
  204.       this.animationAmount = 2;
  205.  
  206.    if (this.animationAmount >= getBrowser().mStrip.boxObject.height)
  207.    {
  208.       this.animationAmount = 0;
  209.       return;
  210.    }
  211.  
  212.    this.animationAmount *= 2;
  213.    getBrowser().mStrip.style.marginTop = (this.animationAmount * -1) + "px";
  214.  
  215.    setTimeout(function() { autoHideTabBar12.animateUpTabbar(); }, 80);
  216. },
  217.  
  218. isEnabled : true,
  219.  
  220. fakeTabbarId : "ahtb12FakeTabbar",
  221. isContextMenuOn : false,
  222. isMouseInTabbar : false,
  223. isTabbarAtBottom : false,
  224.  
  225. animationAmount : 0,  // not used.
  226.  
  227. prefService : null
  228.  
  229. };
  230.  
  231. // delay a little bit to make sure that the browser is up and running.
  232. setTimeout(function(){ autoHideTabBar12.start(); }, 2000);
  233.  
  234.